package com.huluobo.user.view

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_http.Const
import com.huluobo.user.R
import com.hyphenate.chat.EMClient
import com.tencent.mmkv.MMKV

/**
 *  Created by LC on 2025/2/24.
 */
@Composable
fun MinePage(navHostController: NavHostController) {
    val mmkv = MMKV.defaultMMKV()
    var name by remember {
        mutableStateOf(mmkv.decodeString(Const.PARAM_USERNAME, "未登录").toString())
    }

    var role by remember {
        mutableStateOf(mmkv.decodeInt(Const.PARAM_ROLE, 0))
    }

    //控制退出登录的弹窗
    var isShow by remember {
        mutableStateOf(false)
    }

    Column(Modifier.padding(20.dp)) {
        Text(text = "我的", fontSize = 26.sp, fontWeight = FontWeight.Bold)
        Spacer(modifier = Modifier.height(20.dp))
        Card(
            border = BorderStroke(1.dp, Color.LightGray), colors = CardDefaults.cardColors(
                containerColor = Color.White
            ),
            modifier = Modifier
                .fillMaxWidth()
                .clickable {
                    if (name == "未登录") {
                        navHostController.navigate(RoutePath.LOGIN)
                    } else {
                        //弹出退出登录的弹窗
                        isShow = true
                    }
                }
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp)
            ) {
                AsyncImage(
                    model = "https://img0.baidu.com/it/u=788800651,2030194952&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1730307600&t=9c17b588a934b0ccf76349e5e5b7ca08",
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop
                )
                Spacer(modifier = Modifier.width(20.dp))
                Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                    Text(text = name)
                    var roleString = ""
                    if (role == 1) {
                        roleString = "经理"
                    } else if (role == 2) {
                        roleString = "员工"
                    }
                    Text(text = roleString)
                }
            }
        }

        Spacer(modifier = Modifier.height(20.dp))
        Card(
            border = BorderStroke(1.dp, Color.LightGray), colors = CardDefaults.cardColors(
                containerColor = Color.White
            ), modifier = Modifier.fillMaxWidth()
        ) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .padding(20.dp)
            ) {
                Column(Modifier.clickable {
                    navHostController.navigate(RoutePath.APPROVE_LIST)
                }) {
                    Image(
                        painter = painterResource(id = R.drawable.report),
                        contentDescription = "",
                        modifier = Modifier.size(30.dp)
                    )
                    Spacer(modifier = Modifier.height(10.dp))
                    Text(text = "请假")
                }
                Spacer(modifier = Modifier.width(20.dp))
                Column {
                    Image(
                        painter = painterResource(id = R.drawable.question),
                        contentDescription = "",
                        modifier = Modifier.size(30.dp)
                    )
                    Spacer(modifier = Modifier.height(10.dp))
                    Text(text = "审批")
                }
            }
        }
    }

    if (isShow) {
        AlertDialog(onDismissRequest = { isShow = false }, confirmButton = {
            Text(text = "确认", Modifier.clickable {
                EMClient.getInstance().logout(true)
                mmkv.remove(Const.PARAM_USER_UID)
                mmkv.remove(Const.PARAM_USERNAME)
                mmkv.remove(Const.PARAM_PASSWORD)
                mmkv.remove(Const.PARAM_ROLE)
                name = "未登录"
                role = 0
                isShow = false
            })
        }, title = { Text(text = "退出提醒") }, text = { Text(text = "是否退出当前账号") })
    }
}